<script lang="ts" setup name="XtxCity">
import { ref, watchEffect } from 'vue'
import { onClickOutside } from '@vueuse/core'
import { AreaType, AddressType } from './city'
import axios from 'axios'

defineProps({
  address: {
    type: String,
    default: ''
  }
})

const active = ref(false)
const target = ref(null)
// 点击目标之外触发
onClickOutside(target, () => {
  active.value = false
})

const areaList = ref<AreaType[]>([])
const cacheList = ref<AreaType[]>([])

// 收集用户更改的信息
const selectAddress = ref({
  // 省
  provinceName: '',
  provinceCode: '',
  // 市
  cityName: '',
  cityCode: '',
  // 区
  countyName: '',
  countyCode: ''
})

const getAreaList = async () => {
  const res = await axios.get<AreaType[]>(
    'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json'
  )
  areaList.value = res.data
  cacheList.value = res.data // 备份数据 变化的是areaList 当areaList为undefine时 重新赋值
}
getAreaList()

const emits = defineEmits<{
  (e: 'changeAddress', address: AddressType): void
}>()

// 点击省市区
const handleClick = (item: AreaType) => {
  if (item.level === 0) {
    selectAddress.value.provinceName = item.name
    selectAddress.value.provinceCode = item.code
  }

  if (item.level === 1) {
    selectAddress.value.cityName = item.name
    selectAddress.value.cityCode = item.code
  }

  if (item.level === 2) {
    selectAddress.value.countyName = item.name
    selectAddress.value.countyCode = item.code
    active.value = false

    // 将收集的完成地址传递给父组件
    emits('changeAddress', selectAddress.value)
  }
  areaList.value = item.areaList
}

// 下拉框隐藏关闭时重置数据
watchEffect(() => {
  if (!active.value) {
    areaList.value = cacheList.value
  }
})
</script>
<template>
  <div class="xtx-city" ref="target">
    <div class="select" :class="{ active }" @click="active = !active">
      <span class="placeholder" v-if="!address">请选择配送地址</span>
      <span class="value" v-else>{{ address }}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-show="active">
      <span
        class="ellipsis"
        v-for="item in areaList"
        :key="item.code"
        @click="handleClick(item)"
      >
        {{ item.name }}
      </span>
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
